﻿using System;

namespace QuantBox.XApi
{
    internal class SgdQueryManager : QueryManager<QueryResponse>
    {
        private readonly SgdTradeClient _client;

        public SgdQueryManager(SgdTradeClient client)
        {
            _client = client;
        }

        protected override int QueryInstrument(ReqQueryField field)
        {
            return 0;
        }

        protected override int QryInvestorPosition(ReqQueryField field)
        {
            return _client.api!.QryInvestorPosition();
        }

        protected override int QryTradingAccount(ReqQueryField field)
        {
            return _client.api!.QryTradingAccount();
        }

        protected override int QryOrder(ReqQueryField field)
        {
            return 0;
        }

        protected override int QryTrade(ReqQueryField field)
        {
            return 0;
        }

        protected override int QryInstrumentCommissionRate(ReqQueryField field)
        {
            return 0;
        }

        protected override int QryInstrumentMarginRate(ReqQueryField field)
        {
            return 0;
        }

        protected override int QrySettlementInfo(ReqQueryField field)
        {
            return 0;
        }

        protected override int QryInvestor(ReqQueryField field)
        {
            return 0;
        }

        protected override int QryQuote(ReqQueryField field)
        {
            return 0;
        }

        protected override bool ProcessInstrument(QueryResponse rsp)
        {
            return true;
        }

        protected override bool ProcessInvestorPosition(QueryResponse rsp)
        {
            if (rsp.RspInfo.ErrorID == 0)
            {
                var data = rsp.Position;
                PositionField? position = null;
                if (data != null)
                {
                    position = SgdConvert.GetPositionField(data.Value);
                    if (string.IsNullOrEmpty(position.AccountID))
                    {
                        position.AccountID = _client.sgdLoginField.UserID;
                    }
                }
                _client.spi.ProcessQryPosition(position, rsp.IsLast);
                return rsp.IsLast;
            }

            _client.SendError(ref rsp.RspInfo, nameof(ProcessInvestorPosition));
            _client.spi.ProcessQryPosition(null, true);
            return true;
        }

        protected override bool ProcessTradingAccount(QueryResponse rsp)
        {
            if (rsp.RspInfo.ErrorID == 0)
            {
                var data = rsp.Account;
                AccountField? account = null;
                if (data != null)
                {
                    account = SgdConvert.GetAccountField(data.Value);
                }
                _client.spi.ProcessQryAccount(account, rsp.IsLast);
                return rsp.IsLast;
            }

            _client.SendError(ref rsp.RspInfo, nameof(ProcessTradingAccount));
            _client.spi.ProcessQryAccount(null, true);
            return true;
        }

        protected override bool ProcessOrder(QueryResponse rsp)
        {
            throw new NotImplementedException();
        }

        protected override bool ProcessTrade(QueryResponse rsp)
        {
            throw new NotImplementedException();
        }

        protected override bool ProcessInstrumentCommissionRate(QueryResponse rsp)
        {
            throw new NotImplementedException();
        }

        protected override bool ProcessInstrumentMarginRate(QueryResponse rsp)
        {
            throw new NotImplementedException();
        }

        protected override bool ProcessSettlementInfo(QueryResponse rsp)
        {
            throw new NotImplementedException();
        }

        protected override bool ProcessInvestor(QueryResponse rsp)
        {
            throw new NotImplementedException();
        }

        protected override bool ProcessError(QueryResponse rsp)
        {
            throw new NotImplementedException();
        }
    }
}